package stdio.costa.imoca.dao;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Component;

import stdio.costa.imoca.model.MocaUser;

@Component
public class MocaUserDao {

	@PersistenceContext
	private EntityManager entityManager;

	public void save(MocaUser mocaUser) {
		entityManager.persist(mocaUser);
	}

	public MocaUser updateWeiboAccessToken(MocaUser mocaUser) {
		return entityManager.merge(mocaUser);
	}

	public MocaUser findUserByName(String name) {
		Query query = this.entityManager.createNamedQuery("findUserByName")
				.setParameter("name", name);
		try {
			return (MocaUser) query.getSingleResult();
		} catch (NoResultException ne) {
			return null;
		}
	}

	public MocaUser findUserByEmail(String email) {
		Query query = this.entityManager.createNamedQuery("findUserByEmail")
				.setParameter("email", email);
		try {
			return (MocaUser) query.getSingleResult();
		} catch (NoResultException ne) {
			return null;
		}
	}

	public MocaUser findUserByPhone(String phone) {
		Query query = this.entityManager.createNamedQuery("findUserByPhone")
				.setParameter("phone", phone);
		try {
			return (MocaUser) query.getSingleResult();
		} catch (NoResultException ne) {
			return null;
		}
	}

	public MocaUser findUserByWeiboUid(String weiboUid) {

		Query query = this.entityManager.createNamedQuery("findUserByWeiboUid")
				.setParameter("weiboUid", weiboUid);
		try {
			return (MocaUser) query.getSingleResult();
		} catch (NoResultException ne) {
			return null;
		}
	}

	public MocaUser findUserByQQUid(String qqUid) {
		Query query = this.entityManager.createNamedQuery("findUserByQQUid")
				.setParameter("qqUid", qqUid);
		try {
			return (MocaUser) query.getSingleResult();
		} catch (NoResultException ne) {
			return null;
		}
	}

	public MocaUser findUserByWeiboUidAndAccessToken(String weiboUid,
			String weiboAccessToken) {
		Query query = this.entityManager
				.createNamedQuery("findUserByWeiboUidAndAccessToken")
				.setParameter("weiboUid", weiboUid)
				.setParameter("weiboAccessToken", weiboAccessToken);
		MocaUser user = (MocaUser) query.getSingleResult();
		return user;
	}

	public MocaUser findUserByUuidAndPassword(String uuid, String password) {
		Query query = this.entityManager
				.createNamedQuery("findUserByUuidAndPassword")
				.setParameter("uuid", uuid).setParameter("password", password);
		MocaUser user = (MocaUser) query.getSingleResult();
		return user;
	}

	public MocaUser findUserByQQUidAndAccessToken(Long qqUid,
			String qqAccessToken) {
		Query query = this.entityManager
				.createNamedQuery("findUserByQQUidAndAccessToken")
				.setParameter("weiboUid", qqUid)
				.setParameter("qqAccessToken", qqAccessToken);
		MocaUser user = (MocaUser) query.getSingleResult();
		return user;
	}

	public MocaUser findUserByNameOrEmailOrPhone(String name, String email,
			String phone) {
		Query query = this.entityManager
				.createNamedQuery("findUserByNameOrEmailOrPhone")
				.setParameter("name", name).setParameter("email", email)
				.setParameter("phone", phone);
		try {
			return (MocaUser) query.getSingleResult();
		} catch (NoResultException ne) {
			return null;
		}
	}
}
